<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-goods"></i> 商品管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>拼团时间</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row>
                    <el-col :span="4">
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            class="handle-del mr10"
                            @click="delAllSelection"
                        >批量删除</el-button>
                        <el-button
                            type="primary"
                            icon="el-icon-circle-plus-outline"
                            class="handle-del mr10"
                            @click="handleEdit(0,null)"
                        >添加时间</el-button>
                    </el-col>
                    <el-col :span="16" style="display:flex;justify-content: center;">
                        <el-select v-model="query.sort" placeholder="选择排序方式">
                            <el-option key="1" label="正序" value="1"></el-option>
                            <el-option key="2" label="倒序" value="2"></el-option>
                        </el-select>
                        <el-select
                            v-model="query.status"
                            placeholder="状态"
                            class="handle-select mr10"
                        >
                            <el-option label="全部状态" value></el-option>
                            <el-option key="1" label="正常" value="1"></el-option>
                            <el-option key="2" label="禁用" value="2"></el-option>
                        </el-select>
                        <el-select v-model="query.type" placeholder="单位" class="handle-select mr10">
                            <el-option label="全部单位" value></el-option>
                            <el-option key="1" label="天" value="1"></el-option>
                            <el-option key="2" label="时" value="2"></el-option>
                            <el-option key="3" label="分" value="3"></el-option>
                        </el-select>
                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    </el-col>
                </el-row>
            </div>
            <el-table
                :data="tableData"
                border
                v-loading="loading"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" sortable width="55" align="center"></el-table-column>
                <el-table-column label="时间" prop="num" align="center"></el-table-column>
                <el-table-column label="单位" align="center">
                    <template
                        slot-scope="scope"
                    >{{ scope.row.type==3?'分':(scope.row.type==2?'时':'天') }}</template>
                </el-table-column>
                <el-table-column prop="sort" sortable label="排序"></el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.status==1?'success':'danger'"
                        >{{scope.row.status==1?'正常':'禁用'}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="updated_at" label="修改时间"></el-table-column>
                <el-table-column prop="created_at" label="添加时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            @click="updateStatus(scope.row.id,scope.$index,scope.row.status)"
                            :icon="scope.row.status==1?'el-icon-lx-attentionforbid':'el-icon-lx-attention'"
                            type="text"
                            :style="{color:scope.row.status==2?'#67C23A':'#E6A23C'}"
                        >{{ scope.row.status==1?'禁用':'解禁' }}</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 添加编辑弹出 -->
        <el-dialog v-dialogDrag :title="formName" :visible.sync="editVisible" width="480px">
            <el-form :model="edit" label-width="70px">
                <el-form-item label="时间" class="form-item">
                    <el-input
                        type="number"
                        v-model="edit.num"
                        maxlength="30"
                        minlength="2"
                        placeholder="请输入时间"
                    ></el-input>
                </el-form-item>
                <el-form-item label="单位" class="form-item">
                    <el-select v-model="edit.type" placeholder="请选择时间单位">
                        <el-option label="分" value="3"></el-option>
                        <el-option label="时" value="2"></el-option>
                        <el-option label="天" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序" class="form-item">
                    <el-input
                        v-model="edit.sort"
                        maxlength="2"
                        minlength="1"
                        type="number"
                        placeholder="请输入排序"
                    ></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="edit.status" active-value="1" inactive-value="2"></el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <div class="dialog-footer-but">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="handleSave">确 定</el-button>
                </div>
            </span>
        </el-dialog>
    </div>
</template>

<script>

export default {
  name: 'basetable',
  data () {
    return {
      query: {
        type: '',
        page: 1,
        pageSize: 10,
        status: '',
        sort: '1'
      },
      edit: {
        num: '',
        type: '',
        status: '1',
        sort: '10',
      },
      loading: false,
      formName: '',
      editVisible: false,
      tableData: [],
      multipleSelection: [],
      pageTotal: 0,
      delList: [],
      idx: -1,
      id: -1,
      //获取列表请求
      getList: function () {
        let that = this;
        this.api.get('goods-time', this.query, res => {
          for (let i in res.data.list) {
            res.data.list[i].status = res.data.list[i].status + '';
          }
          that.tableData = res.data.list;
          that.query.page = res.data.page;
          that.query.pageSize = res.data.pageSize;
          that.pageTotal = res.data.pageTotal;
        }, undefined, undefined, this);
      },
      //删除请求
      delLists: function (ids, msg) {
        let that = this;
        if (ids.length < 1) {
          this.$message.error('请选择删除的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.delete('delGoodsUnitTime', { ids: ids }, res => {
            that.query.page = 1;
            that.getList();
            that.multipleSelection = [];
            that.$message.success(res.msg);
          }, undefined, undefined, this);
        }).catch(() => { });
      }
    };
  },
  created () {
    this.getList();

  },
  methods: {
    // 触发搜索按钮
    handleSearch () {
      this.$set(this.query, 'page', 1);
      this.getList();
    },

    /**
     * 操作状态
     */
    updateStatus (id, index, status) {
      this.$confirm('确定要' + (status == 1 ? '禁用' : '解禁') + '吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.api.put('goodsTimeStatus', { id: id }, res => {
          this.tableData[index].status = status == 1 ? 2 : 1;
          this.$message.success(res.msg);
        });
      }).catch(() => { });

    },

    /**
     * 添加编辑
     */
    handleEdit (index, row) {
      if (row === null) {
        this.formName = '添加拼团时间';
        this.edit = {
          num: '',
          type: '',
          status: '1',
          sort: '10',
        };
      } else {
        this.edit = row;
        this.formName = '编辑拼团时间';
      }
      this.editVisible = true;
    },

    /**
     * 保存数据
     */
    handleSave () {
      let uri = 'addGoodsTime', method = 'POST';
      if (typeof this.edit.id != 'undefined') {
        uri = 'editGoodsTime';
        method = 'PUT';
      }
      this.api.requests(uri, method, this.edit, res => {
        this.$message.success(res.msg);
        this.query.page = 1;
        this.getList();
        this.editVisible = false;
      }, undefined, undefined, this);
    },

    // 多选操作
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },

    // 删除操作
    handleDelete (index, row) {
      this.delLists([row.id], '删除成功');
    },
    //批量删除
    delAllSelection () {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
        ids.push(this.multipleSelection[i].id);
      }
      this.delLists(ids, `删除了${str}`);
    },

    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    }
  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
